package com.qf.business.system.controller;

import com.qf.business.system.service.PowerService;
import com.qf.business.system.service.RolePowerTableService;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.base.utilis.QfBeanUtils;
import com.qf.data.system.dto.PowerDto;
import com.qf.data.system.entity.Power;
import com.qf.data.system.entity.RolePowerTable;
import com.qf.data.system.vo.input.PowerInput;
import com.qf.data.system.vo.output.PowerDtoOutput;
import com.qf.data.system.vo.output.PowerSimpleOutput;
import com.qf.data.system.vo.output.PowerTreeNodeOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/power")
public class PowerController {
    @Autowired
    private PowerService powerService;

    @Autowired
    private RolePowerTableService rolePowerTableService;

    @RequestMapping("/list")
    public R list() {
        List<PowerDto> powerDtos = powerService.queryList();
        List<PowerDtoOutput> outputs = QfBeanUtils.listCopy(powerDtos, PowerDtoOutput.class);

        return Rs.create(outputs);
    }

    @RequestMapping("/insert")
    public R insert(@Validated PowerInput input) {
        Power power = QfBeanUtils.beanCopy(input, Power.class);
        powerService.save(power);
        return Rs.create("新增权限成功");
    }

    @RequestMapping("/insertByList")
    public R insertByList() {
        List<PowerDto> powerDtos = powerService.queryList();
        List<PowerSimpleOutput> outputs = QfBeanUtils.listCopy(powerDtos, PowerSimpleOutput.class);
        return Rs.create(outputs);
    }

    @RequestMapping("/listByTree")
    public R listByTree() {
        //根节点
        List<PowerTreeNodeOutput> rootNodeList = new ArrayList<>();
        //id对应的节点
        Map<Long, PowerTreeNodeOutput> treeNodeMap = new HashMap<>();
        //查询所有权限列表
        List<Power> powers = powerService.query().orderByAsc("pid").list();
        //将power转化为PowerTreeNodeOutput
        for (Power power : powers) {
            PowerTreeNodeOutput treeNode = new PowerTreeNodeOutput()
                    .setId(power.getId())
                    .setLabel(power.getPowerName())
                    .setType(power.getType());

            if (power.getPid() != null) {
                //判断是否是子节点,如果是顶级节点则从map集合查询到父节点
                PowerTreeNodeOutput parentTreeNode = treeNodeMap.get(power.getPid());
                parentTreeNode.addChildren(treeNode);
            } else {
                rootNodeList.add(treeNode);
            }
            treeNodeMap.put(treeNode.getId(),treeNode);

        }
        List<PowerTreeNodeOutput> nodes = rootNodeList;
        return Rs.create(nodes);
    }

    @RequestMapping("/listByRid")
    public R listByRid(Long rid) {
        List<RolePowerTable> rolePowerTables = rolePowerTableService.queryByRid(rid);
        List<Long> pids = rolePowerTables.stream()
                .map(rolePowerTable -> rolePowerTable.getPid())
                .collect(Collectors.toList());
        return Rs.create(pids);
    }

}
